package com.tuvi.nhamthin.activity;

import java.util.Calendar;

import com.tuvi.nhamthin.R;
import com.tuvi.nhamthin.entity.GiapItem;
import com.tuvi.nhamthin.helper.SqlHelper;
import com.tuvi.nhamthin.services.BackgroundMusic;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class About extends BaseActivity{
	private WebView mWebView;
	 private int mYear;
	 private int mMonth;
	 private int mDay;
	 private EditText et;
	 /** Button Back */
	 private ImageButton mBtnBack;
	 // the callback received when the user "sets" the date in the dialog
	 private DatePickerDialog.OnDateSetListener mDateSetListener =
	            new DatePickerDialog.OnDateSetListener() {

	                public void onDateSet(DatePicker view, int year, 
	                                      int monthOfYear, int dayOfMonth) {
	                    mYear = year;
	                    mMonth = monthOfYear;
	                    mDay = dayOfMonth;
	                    updateDisplay();
	                }
	            };
	
	 static final int DATE_DIALOG_ID = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.about);
	    mWebView = (WebView) findViewById(R.id.webview);
	    mWebView.getSettings().setJavaScriptEnabled(true);
	    mWebView.loadUrl("file:///android_asset/gioithieu.html");
	    mWebView.setWebViewClient(new HelloWebViewClient());
	    et = (EditText)findViewById(R.id.date);
	    et.setInputType(InputType.TYPE_NULL);
	    et.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	showDialog(DATE_DIALOG_ID);
            }
        });
	    mBtnBack = (ImageButton) findViewById(R.id.btn_back);
	    mBtnBack.setOnClickListener(new OnClickListener() {
	    		@Override
	    	    public void onClick(View v) {
	    	       // your code here
	    	    	finish();
	    	    }
	    });
	    TextView search = (TextView)findViewById(R.id.btn_search);
	    search.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				findAge();
			}
		});
	    // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        
        // display the current date (this method is below)
        updateDisplay();


	}
	
	private void findAge(){
		SqlHelper sql = new SqlHelper(this);
		Cursor cr = sql.findAge(et.getText().toString());
		startManagingCursor(cr);
		if (cr.getCount() > 0) {
			//Point to 1st record
			cr.moveToNext();
			GiapItem item = new GiapItem(
					cr.getString(cr.getColumnIndex(SqlHelper.COLUMN_NAME)),
					cr.getString(cr.getColumnIndex(SqlHelper.COLUMN_FULLNAME)),
					null,
					null,
					cr.getString(cr.getColumnIndex(SqlHelper.COLUMN_PARENT)),
					null,
					null,
					null,
					null,
					null);
			if (item.getParent() != null) {
				Intent i = new Intent(this, Giap.class);
				i.putExtra("name", item.getName());
				i.putExtra("parent", item.getParent());
				i.putExtra("full_name", item.getFullName());
				startActivity(i);
			}
		}else{
			Toast alert = Toast.makeText(this, this.getString(R.string.no_result), 1000);
			alert.show();
		}
	}
	   // updates the date in the TextView
    private void updateDisplay() {
        et.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
            		.append(mYear).append("-")
                    .append(String.valueOf(mMonth+1).length() < 2?"0"+String.valueOf(mMonth+1):mMonth+1).append("-")
                    .append(String.valueOf(mDay).length() <2 ? "0"+String.valueOf(mDay):mDay)
                    );
    }
	
	private class HelloWebViewClient extends WebViewClient {
	    @Override
	    public boolean shouldOverrideUrlLoading(WebView view, String url) {
	        view.loadUrl(url);
	        return true;
	    }
	}

	

	@Override
	protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    case DATE_DIALOG_ID:
	    	DatePickerDialog datePic =  new DatePickerDialog(this,
	                    mDateSetListener,
	                    mYear, mMonth, mDay);
	    	
	    	return datePic;
	    }
	    return null;
	}
    @Override
    public void onStop(){
    	super.onStop();
    }
    @Override
    public void onResume(){
    	super.onResume();
    }
}
